package com.lft.com.oilgas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lft.com.oilgas.entity.Driver;
import com.lft.com.oilgas.entity.Merchant;
import com.lft.com.oilgas.entity.Role;
import com.lft.com.oilgas.entity.User;
import com.lft.com.oilgas.mapper.DriverMapper;
import com.lft.com.oilgas.mapper.MerchantMapper;
import com.lft.com.oilgas.mapper.RoleMapper;
import com.lft.com.oilgas.mapper.UserMapper;
import com.lft.com.oilgas.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tyqw
 * @since 2024-09-10
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    MerchantMapper merchantMapper;
    @Autowired
    DriverMapper driverMapper;

    //添加用户
    @Override
    public int insertUser(User user) {
        System.out.println(user);
        userMapper.insert(user);
        if (user.getRoleId() == 1) {
            Driver driver = new Driver();
            driver.setUserId(user.getId());
            return driverMapper.insert(driver);
        } else if (user.getRoleId() == 2) {
            Merchant merchant = new Merchant();
            merchant.setUserId(user.getId());
            return merchantMapper.insert(merchant);
        }
        return 0;
    }


    //登录
    @Override
    public User selectUser(User user) {
        System.out.println(user);
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("phone", user.getPhone());
        qw.eq("password", user.getPassword());
        qw.eq("role_id", user.getRoleId());
        return userMapper.selectOne(qw);
    }

    @Override
    public int updateUser(User user) {
        int i;
        if (user!=null){
            i = userMapper.updateById(user);
        }else {
             i=0;
        }
        return i;
    }

    @Override
    public User selsectByid(Integer id) {
        return  userMapper.selectById(id);
    }
}
